package com.crm.business.agreement.service.impl;

import com.ancun.netsign.model.UserSeal;
import com.ancun.netsign.model.UserSealOutput;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.business.agreement.mapper.BillAgreementSealSubjectMapper;
import com.crm.business.agreement.service.IBillAgreementSealSubjectService;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.business.utils.UserUtil;
import com.crm.common.agreement.common.AiQianUtils;
import com.crm.common.agreement.entity.BO.SealSubjectBO;
import com.crm.common.agreement.entity.PO.BillAgreementSealSubject;
import com.crm.common.core.domain.model.LoginUser;
import com.crm.common.exception.CustomException;
import com.crm.common.ka.core.common.SystemCodeEnum;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.common.ka.core.entity.UserInfo;
import com.crm.common.utils.DateUtils;
import com.crm.common.utils.SecurityUtils;
import com.crm.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@Slf4j
@Service
public class BillAgreementSealSubjectServiceImpl extends BaseServiceImpl<BillAgreementSealSubjectMapper, BillAgreementSealSubject> implements IBillAgreementSealSubjectService {

    @Autowired
    private AiQianUtils aiQianUtils;

    @Override
    public int add(BillAgreementSealSubject model) {

        if(!Optional.ofNullable(model).isPresent()
                || StringUtils.isEmpty(model.getSealName())
                || StringUtils.isEmpty(model.getBusinessLicenseNum())
                || StringUtils.isEmpty(model.getEnterpriseName())
                || StringUtils.isEmpty(model.getCorporationName())
                || StringUtils.isEmpty(model.getCorporationCard())
                || StringUtils.isEmpty(model.getCorporationPhone())
                || StringUtils.isEmpty(model.getContactsName())
                || StringUtils.isEmpty(model.getContactsCard())
                || StringUtils.isEmpty(model.getContactsPhone())){
            throw new CustomException(SystemCodeEnum.SYSTEM_NO_VALID.getMsg(),SystemCodeEnum.SYSTEM_NO_VALID.getCode());
        }
        //创建企业用户
        String orgAccount = aiQianUtils.addEnterpriseUser(model.getEnterpriseName(),
                model.getBusinessLicenseNum(),
                model.getCorporationName(),
                model.getCorporationPhone(),
                model.getCorporationCard(),
                model.getContactsName(),
                model.getContactsPhone(),
                model.getContactsCard());
        //爱签印章
        String sealNo = aiQianUtils.createSeal(orgAccount,model.getSealName());
        UserInfo loginUser = UserUtil.getUser();
        model.setAccount(orgAccount);
        model.setSealNo(sealNo);
        model.setCreateUserId(loginUser.getUserId());
        model.setCreateUserName(loginUser.getRealname());
        model.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insert(model);

        return i;
    }

    @Override
    public int update(BillAgreementSealSubject model) {

        if(!Optional.ofNullable(model).isPresent() || !Optional.ofNullable(model.getSubjectId()).isPresent()
                || StringUtils.isEmpty(model.getSealName())
                || StringUtils.isEmpty(model.getBusinessLicenseNum())
                || StringUtils.isEmpty(model.getEnterpriseName())
                || StringUtils.isEmpty(model.getCorporationName())
                || StringUtils.isEmpty(model.getCorporationCard())
                || StringUtils.isEmpty(model.getCorporationPhone())
                || StringUtils.isEmpty(model.getContactsName())
                || StringUtils.isEmpty(model.getContactsCard())
                || StringUtils.isEmpty(model.getContactsPhone())){
            throw new CustomException(SystemCodeEnum.SYSTEM_NO_VALID.getMsg(),SystemCodeEnum.SYSTEM_NO_VALID.getCode());
        }
        //创建企业用户
        String orgAccount = aiQianUtils.addEnterpriseUser(model.getEnterpriseName(),
                model.getBusinessLicenseNum(),
                model.getCorporationName(),
                model.getCorporationPhone(),
                model.getCorporationCard(),
                model.getContactsName(),
                model.getContactsPhone(),
                model.getContactsCard());
        //爱签印章
        String sealNo = aiQianUtils.createSeal(orgAccount,model.getSealName());
        UserInfo loginUser = UserUtil.getUser();
        model.setAccount(orgAccount);
        model.setSealNo(sealNo);
        model.setCreateUserId(loginUser.getUserId());
        model.setCreateUserName(loginUser.getRealname());
        model.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insert(model);

        return i;
    }

    public IPage<BillAgreementSealSubject> queryPageList(SealSubjectBO model){

        IPage<BillAgreementSealSubject> page = new Page<>(model.getPage(), model.getLimit());
        QueryWrapper<BillAgreementSealSubject> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(model.getEnterpriseName())){
            queryWrapper.like("enterprise_name",model.getEnterpriseName());
        }
        if(StringUtils.isNotEmpty(model.getSealNo())){
            queryWrapper.eq("seal_no",model.getSealNo());
        }
        if(StringUtils.isNotEmpty(model.getSealName())){
            queryWrapper.like("seal_name",model.getSealName());
        }
        queryWrapper.orderByDesc( "subject_id");
        IPage<BillAgreementSealSubject> pageInfo = baseMapper.selectPage(page, queryWrapper);
        return pageInfo;
    }

    @Override
    public List<BillAgreementSealSubject> queryList(SealSubjectBO model){

        QueryWrapper<BillAgreementSealSubject> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(model.getEnterpriseName())){
            queryWrapper.like("enterprise_name",model.getEnterpriseName());
        }

        queryWrapper.orderByDesc( "subject_id");
        List<BillAgreementSealSubject> list = baseMapper.selectList(queryWrapper);
        return list;
    }



    @Override
    public Map<String,Object> getSeal(Long id){

        BillAgreementSealSubject subject = baseMapper.selectById(id);
        if(!Optional.ofNullable(subject).isPresent()){
            throw new CustomException(SystemCodeEnum.SYSTEM_NO_VALID.getMsg(),SystemCodeEnum.SYSTEM_NO_VALID.getCode());
        }
        UserSealOutput sealOutput = aiQianUtils.getUserSeals(subject.getAccount(),subject.getSealNo());
        UserSeal userSeal = sealOutput.getList().stream().findFirst().get();
        String sealUrl = userSeal.getSealUrl();
        Map<String,Object> result = new HashMap<>();
        result.put("url",sealUrl);
        return result;
    }
}
